﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;

namespace Sunboxsoft.ASMS.Client.UI.Management
{
    /// <summary>
    /// 客户列表
    /// </summary>
    public partial class CustomerListView : UserControl, IView
    {
        #region fileds
        private int _pageIndex;
        private int _pageCount;
        Promise _promise = new Promise();
        #endregion

        public CustomerListView()
        {
            InitializeComponent();
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection
                .AddAction(new ActionNode("view", Resources.isView, "查看", ctlBtnView_ItemClick))
                .AddAction(new ActionNode("create", Resources.isCreateCustomer, "新增", ctlBtnCreate_ItemClick))
                .AddAction(new ActionNode("edit", Resources.isEdit, "修改", ctlBtnEdit_ItemClick))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_ItemClick));
        }

        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                RefreshData();
            }
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(RefreshData).Run();
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("客户列表");
        }
        #endregion

        #region actions
        /// <summary>
        /// 查看
        /// </summary>
        private void ctlBtnView_ItemClick(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VCustomer item = this.GVData.GetFocusedRow() as WebRef.VCustomer;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Show.ShowCustomerDetailView(item.Id), new PopupViewArgs("查看客户详细信息"));
                }
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnCreate_ItemClick(object sender, object e)
        {
            UIContext.Current.ViewManager.PopupView(new Create.CreateCustomerView(), new PopupViewArgs("新增客户"));
        }
        /// <summary>
        /// 修改
        /// </summary>
        private void ctlBtnEdit_ItemClick(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VCustomer item = this.GVData.GetFocusedRow() as WebRef.VCustomer;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Edit.UpdateCustomerView(item.Id), new PopupViewArgs("修改客户"));
                }
            }
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_ItemClick(object sender, object e)
        {
            _pageIndex = 0;
            RefreshData();
        }
        private void ctlGrid_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
        }
        #endregion

        #region methods
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void RefreshData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                string filter = ctlEditFilter.EditValue == null ? null : ctlEditFilter.EditValue.ToString();
                PagedResult<WebRef.VCustomer> result = new Service.QueryService().QueryCustomerPagedList(filter, _pageIndex * GetPageLimit(), GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ctlGrid.Paginate<WebRef.VCustomer>(result, out _pageIndex, out _pageCount);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError("查询失败！");
                    }
                    this.HideMask();
                }));
            });
        }

        public int GetPageLimit()
        {
            return 25;
        }
        #endregion
    }
}
